package com.example.car.volley.request;

import java.io.UnsupportedEncodingException;

import org.json.JSONException;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.car.volley.VolleyListen;

public class VolleyRequest  extends StringRequest{
	private VolleyListen listen;
	public VolleyRequest(int method, String url,final VolleyListen listener
			) {
		super(method, url, new Listener<String>() {
			@Override
			public void onResponse(String response) {
				if (listener != null) {
					listener.onResponse();
				}
			}
			
			
		},new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if(listener!=null){
					listener.onError(error);
				}
			}
		});
		listen=listener;
	}

	@Override
	protected Response<String> parseNetworkResponse(NetworkResponse response) {
		String newStr;
		try {
			newStr=new String(response.data, "utf-8");
			
		} catch (UnsupportedEncodingException e) {
			newStr=new String(response.data);
		}
		if(listen!=null){
			try {
				listen.onParse(newStr);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return super.parseNetworkResponse(response);
	}

}
